package com.anttribe.components.db.sql.statement;

import com.anttribe.components.db.sql.format.SqlFormatter;
import org.apache.commons.lang3.StringUtils;

import com.anttribe.components.db.sql.stereotype.SqlStatement;

import lombok.Getter;
import lombok.Setter;

/**
 * @author zhaoyong
 * @date 2022/11/21 0021
 */
@Getter
@Setter
public abstract class AbstractSqlStatement implements SqlStatement, SqlFormatter {

    /**
     * sql格式化选项
     */
    protected SqlFormatter.FormatOptions formatOptions;

    public AbstractSqlStatement() {
        formatOptions = SqlFormatter.FormatOptions.builder().build();
    }

    @Override
    public String generateSql() {
        return this.generateSql(formatOptions);
    }

    @Override
    public String generateSql(SqlFormatter.FormatOptions formatOptions) {
        if (null != formatOptions) {
            this.formatOptions = formatOptions;
        }

        String sql = this.generateSqlInner();
        if (!StringUtils.isEmpty(sql)) {
            if (this.formatOptions.isEndWithSemicolon()) {
                sql = sql + SYMBOL_SEMICOLON;
            }
        }

        return sql;
    }

    /**
     * 构造sql
     *
     * @return sql
     */
    protected abstract String generateSqlInner();

}
